package com.zdkj.admin.job;/**
 * @author 陈博
 * @date 2020/5/14 0014 14:30
 */

import com.zdkj.admin.api.dto.NoticeOutDTO;
import com.zdkj.admin.api.entity.SysUser;
import com.zdkj.common.data.tenant.TenantContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @program: zdkj
 *
 * @description:   通知推送
 *
 * @author: chen
 *
 * @create: 2020-05-14 14:30
 **/
@Component
@Slf4j
public class SysNoticeJob {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;



    @Async
    public void sendGroup(List<SysUser> list, NoticeOutDTO noticeOutDTO){
    list.forEach(user ->{
        String target = String.format("%s-%s", user.getUserCode(), user.getTenantId());
        simpMessagingTemplate.convertAndSendToUser(target,"/myNotice",noticeOutDTO);
    });

    }

    @Async
    public void sendUser(SysUser sysUser, NoticeOutDTO noticeOutDTO){
            String target = String.format("%s-%s", sysUser.getUserCode(), TenantContextHolder.getTenantId());
            simpMessagingTemplate.convertAndSendToUser(target,"/myNotice",noticeOutDTO);

    }
}
